<?php

require FCPATH . '/application/controllers/backend/backend.php';

class Node extends Backend {

    public function __construct() {
	parent::__construct();
	$this->load->model('node_model', 'model');
    }

    public function index() {
	$this->load->config('global');

	$data = array(
	    'types' => $this->config->item('image_types'),
	    'l_position' => array(
		array('name' => '图片管理')
	    )
	);
	$this->load_js(array('jquery-ui-1.8.20.custom.min', 'backend_node'));
	$this->load_css(array('custom-theme/jquery-ui-1.8.20.custom', 'node'));
	$this->rend('node', $data);
    }

    public function records() {
	$page = intval($this->input->get('page'));
	$page_size = intval($this->input->get('pageSize'));
	$page = $page ? $page : 1;
	$page_size = $page_size ? $page_size : 10;
	$offset = $page_size * ($page - 1);

	$conditions = array();
	$parents = array();

	$id = intval($this->input->get('id'));
	if ($id) {
	    $conditions[] = array('where', 'id', $id);
	}

	$ids = trim($this->input->get('ids'));
	if(!empty($ids)){
	    $conditions[] = array('where_in', 'id', explode(',',$ids));
	}
	
	$parent = intval($this->input->get('parent'));
	if ($parent != -1) {
	    $conditions[] = array('where', 'parent', $parent);

	    if ($parent > 0) {
		$this->get_parents($parent, $parents);
	    }
	    $parents[] = array('id' => 0, 'name' => '顶级');

	    $this->load->helper('array');
	    
	    z_rsort($parents);
	}

	$type = intval($this->input->get('type'));
	if (!empty($type)) {
	    $conditions[] = array('where', 'type', $type);
	}

	$label = trim($this->input->get('label'));
	if (!empty($label)) {
	    $conditions[] = array('like', 'label', $label);
	}

	$start = $this->input->get('start');
	if (!empty($start)) {
	    $conditions[] = array('where', 'created_at >=', $start);
	}

	$end = $this->input->get('end');
	if (!empty($end)) {
	    $conditions[] = array('where', 'created_at <=', $end);
	}

	$order_fields = array('id', 'parent', 'type', 'label', 'name', 'created_at');
	$order_field = intval($this->input->get('order_field'));
	$order_type = intval($this->input->get('order_type'));
	$orders = array();
	if ($order_field >= 0 && $order_field < count($order_fields)) {
	    $orders = array(
		array($order_fields[$order_field], $order_type == 1 ? 'asc' : 'desc')
	    );
	}
	
	$this->json_back('XT001', '', array(
	    'parents' => $parents,
	    'total' => $this->model->total($conditions),
	    'records' => $this->model->records(array(), $conditions, $orders, $page_size, $offset)
	));
    }

    public function rename() {
	$id = intval($this->input->post('id'));
	$parent = intval($this->input->post('parent'));

	$data = array(
	    'label' => trim($this->input->post('label'))
	);

	$conditions = array(
	    array('where', 'parent', $parent),
	    array('where', 'label', $data['label'])
	);

	if ($this->model->total($conditions) > 0) {
	    $this->json_back('XT002', '该目录不能有重名的文件');
	}

	if ($this->model->update($id, $data)) {
	    $this->json_back('XT001', '重命名成功', array('id' => $id));
	}

	$this->json_back('XT002', '数据库或SQL语句有误。');
    }

    public function paste() {
	$data = array();
	$parent = $this->input->post('parent');
	$ids = $this->input->post('id');

	if (isset($ids[0])) {
	    foreach ($ids as $id) {
		$data[] = array(
		    'id' => $id,
		    'parent' => $parent
		);
	    }
	} else {
	    $this->json_back('XT001', '粘帖板为空');
	}

	$this->model->update_batch($data, 'id');

	$this->json_back('XT001', '粘帖成功');
    }

    private function get_parents($id, &$result) {
	if (($row = $this->model->find($id))) {
	    $result[] = array(
		'id' => $row->id,
		'name' => $row->label
	    );

	    if ($row->parent > 0) {
		$this->get_parents($row->parent, $result);
	    }
	}
    }

}
